﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    EnemyBaseState currentState;

    public Animator anim;
    public int animState;

    private GameObject alamsign;

    [Header("Base State")]
    public float health;
    public bool isDead;
    public bool hasBomb;
    public bool isBoss; 

    [Header("Movement")]
    public float speed;
    public Transform pointA, pointB;
    public Transform targetPoint;

    [Header("Attack Setting")]
    
    public float attackRate;
    private float nextAttack = 0;
    public float attackRange, skillRange; 


    public List<Transform> attackList = new List<Transform>();

    public PatroState patroState = new PatroState();
    public AttackState attackState = new AttackState();

     public virtual void Init()//virtrual 虚函数，可以修改，方便调用
    {
        anim = GetComponent<Animator>();
        alamsign = transform.GetChild(0).gameObject;

       
       
    }
    public void Awake()//先于START，不会丢组件
    {
        Init();
    }
    // Start is called before the first frame update
    void Start()
    {
        TransitionToState(patroState);
        if (isBoss)
            UIManager.instance.SetBossHealth(health);
        GameManager.instance.IsEnemy(this);

    }

    // Update is called once per frame
    public virtual void Update()
    {
        anim.SetBool("dead", isDead);
        if (isDead)
        {
            GameManager.instance.EnemyDead(this);
            return;
        }
           
        
        currentState.OnUpdate(this);
        anim.SetInteger("state", animState);

        if (isBoss)
            UIManager.instance.UpadateBossHealthbar(health);

    }
    public void TransitionToState(EnemyBaseState state)
    {
        currentState = state;
        currentState.EnterState(this);
    }
    public void MoveToTarget()
    {
        transform.position = Vector2.MoveTowards(transform.position, targetPoint.position, speed * Time.deltaTime);
        Flip();
    }
    public void Flip()//翻转
    {
        if (transform.position.x < targetPoint.position.x)
            transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        else transform.rotation = Quaternion.Euler(0f,180f, 0f);
    }
    public void AttackAction()//攻击玩家
    {
        if (Vector2.Distance(transform.position, targetPoint.position) < attackRange) 
        {
            if(Time .time >nextAttack)
            {
                anim.SetTrigger("attack");
                Debug.Log("普通攻击");
                nextAttack = Time.time + attackRate;
            }
        }
    }
    public virtual void SkillAction()//黄瓜怪的技能
    {
        if (Vector2.Distance(transform.position, targetPoint.position) < skillRange)
        {
            if (Time.time > nextAttack)
            {
                anim.SetTrigger("skill");
                Debug.Log("这是技能攻击");
                nextAttack = Time.time + attackRate;
            }
        }
    }
    public void SwitchPoint()
    {
        if (Mathf.Abs(pointA.position.x - transform.position.x) >Mathf.Abs(pointB.position.x - transform.position.x))
        {
            targetPoint = pointA;
        }
        else
        {
            targetPoint = pointB;
        }
     }
    public void OnTriggerStay2D(Collider2D collision)
    {
        if (!attackList .Contains(collision.transform) && !hasBomb &&!isDead && !GameManager .instance .gameOver )
             attackList.Add(collision.transform);

    }
    public void OnTriggerExit2D(Collider2D collision)
    {
        attackList.Remove(collision.transform);
    }
    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (!isDead&&!GameManager.instance.gameOver)
        StartCoroutine(Onalam());
    }
    IEnumerator Onalam()
    {
        alamsign.SetActive(true);
        yield return new WaitForSeconds(alamsign.GetComponent<Animator>().GetCurrentAnimatorClipInfo(0)[0].clip.length);
        alamsign.SetActive(false);
    }
}
    